package com.zyao89.view.zloading.base;


import com.zyao89.view.zloading.ZLoadingBuilder;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.ColorFilter;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Created by zyao89 on 2018/3/10.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: https://zyao89.cn
 */
public abstract class BaseStateBuilder extends ZLoadingBuilder {
    /**
     * 当前动画阶段
     */
    private int mCurrAnimatorState = 0;
    private Paint mPaint;


    /**
     * 总阶段数
     *
     * @return 数字
     */
    protected abstract int getStateCount();

    /**
     * 初始化画笔
     *
     * @param context
     * @param paint
     */
    protected abstract void initParams(Context context, Paint paint);

    /**
     * 动画数值更新
     *
     * @param animation
     * @param animatedValue
     * @param state         当前状态
     */
    protected abstract void onComputeUpdateValue(AnimatorValue animation, float animatedValue, int state);

    @Override
    protected final void initParams(Context context, Color color) {
        initPaint(color);
        this.initParams(context, mPaint);
    }

    private void initPaint(Color color) {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setStrokeWidth(6);
        mPaint.setColor(color);
        mPaint.setDither(true);
        mPaint.setFilterBitmap(true);
        mPaint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        mPaint.setStrokeJoin(Paint.Join.ROUND_JOIN);
    }

    @Override
    protected void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    protected void computeUpdateValue(AnimatorValue animation, float animatedValue) {
        this.onComputeUpdateValue(animation, animatedValue, mCurrAnimatorState);
    }

    @Override
    public void onRepeat(Animator animator) {
        int iFinalState = getStateCount();
        if (++mCurrAnimatorState > iFinalState) {//还原到第一阶段
            mCurrAnimatorState = 0;
        }
    }

    @Override
    protected void setColorFilter(ColorFilter colorFilter) {
        mPaint.setColorFilter(colorFilter);
    }

}
